﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Threading;
using System.Windows;
using Kokomo.Environment.Hosting.Settings;

namespace Kokomo.Environment.Hosting.Services
{
	class AutoSaveService : ServiceObjectBase
	{
		public AutoSaveService(EditorApplication editorApplication)
			: base(editorApplication)
		{
			TimeSpan interval = TimeSpan.FromMinutes(5);
			this.AutoSaveTimer = new DispatcherTimer();
			this.AutoSaveTimer.Tick += new EventHandler(AutoSaveTimer_Tick);

			this.LoadSettings();
		}

		public string AutoSavePath { get; private set; }

		protected void LoadSettings()
		{
			AutoSaveSettings settings = this.EditorApplication.SettingsManager.GetSettings<AutoSaveSettings>();
			if (settings == null) return;
			this.AutoSavePath = settings.AutoSavePath;
			this.AutoSaveTimer.Interval = settings.Interval;
			this.AutoSaveTimer.IsEnabled = settings.IsEnabled;
		}

		public void Perform()
		{
			foreach (var document in this.EditorApplication.DocumentManager.DocumentViewModels)
			{
				if (!document.IsDirty) continue;

				string fileName = document.FileName;

			}
		}

		void AutoSaveTimer_Tick(object sender, EventArgs e)
		{
			throw new NotImplementedException();
		}

		public DispatcherTimer AutoSaveTimer { get; private set; }

		public static string GetAutoSaveFileName(DependencyObject obj)
		{
			return (string)obj.GetValue(AutoSaveFileNameProperty);
		}

		public static void SetAutoSaveFileName(DependencyObject obj, string value)
		{
			obj.SetValue(AutoSaveFileNameProperty, value);
		}

		// Using a DependencyProperty as the backing store for AutoSaveFileName.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty AutoSaveFileNameProperty =
			DependencyProperty.RegisterAttached("AutoSaveFileName", typeof(string), typeof(AutoSaveService), new UIPropertyMetadata(null));


	}
}
